package com.Jin.config;

import com.Jin.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class IdentifyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断权限
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if(user != null){
            if (request.getRequestURI().contains("usermain") &&  "管理员".equals(user.getIdentify())){
                response.sendRedirect("/rootmain");
            }
            if (request.getRequestURI().contains("rootmain") &&  "用户".equals(user.getIdentify())){
                response.sendRedirect("/usermain");
            }
            return true;
        }

        return true;
    }
}
